package com.lxz.bookmanager.controller.admin;

import com.lxz.bookmanager.entity.Borrow;
import com.lxz.bookmanager.entity.Reader;
import com.lxz.bookmanager.mapper.BookMapper;
import com.lxz.bookmanager.entity.Book;
import com.lxz.bookmanager.service.BookService;
import com.lxz.bookmanager.service.BorrowService;
import net.minidev.json.parser.JSONParser;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/admin/book")
public class AdminBookController {


    @Autowired
    private BookService bookService;

    @Autowired
    private BorrowService borrowService;


    @RequestMapping("/getAll")
    public String getAll(Model model) {
        List<Book> books = bookService.getAllBook();
        model.addAttribute("books", books);
        return "admin/books";
    }

    @RequestMapping("/info/{id}")
    public String info(Model model, @PathVariable("id") String id) {
        Book book = bookService.getBookById(id);
        int count = bookService.selectBookCount(id);
        if (count > 0) {
            book.setState(1);
        }
        model.addAttribute("book", book);
        return "admin/book_info";
    }

    @RequestMapping("/toEditPage/{id}")
    public String toEditPage(Model model, @PathVariable("id") String id) {
        Book book = bookService.getBookById(id);
        model.addAttribute("book", book);
        return "admin/book_edit";
    }

    @RequestMapping("/update")
    public String update(Book book) {
        bookService.updateBook(book);
        return "redirect:/admin/book/getAll";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") long id) {
        bookService.deleteBook(id);
        return "redirect:/admin/book/getAll";
    }

    @RequestMapping("/toAddPage")
    public String toAddPage() {
        return "admin/book_add";
    }

    @RequestMapping("/add")
    public String add(Book book) {
        bookService.addBook(book);
        return "redirect:/admin/book/getAll";
    }

    @RequestMapping("/search")
    public String search(String keyword, Model model) {
        List<Book> books = bookService.searchBook(keyword);
        model.addAttribute("books", books);
        return "admin/book_search_result";
    }

    //申请借阅图书
    @RequestMapping("/borrow/getAll")
    public String borrow(Model model) {
        List<Borrow> borrows = borrowService.getAllBorrow();
        model.addAttribute("borrows",borrows);
        return "admin/book_borrow";
    }
    @RequestMapping("/borrow/accept/{sernum}")
    public String borrowaccept(Model model,@PathVariable("sernum") int sernum) {
        borrowService.acceptBorrow(sernum);
        return "admin/book_borrow";
    }
}
